﻿using B.S.DataBase.SuYuan.Domains.Entites;
using B.S.DataBase.SuYuan.ErrorCode;
using B.S.DataBase.SuYuan.Infrastructure.Interfaces;
using B.S.DataBase.SuYuan.Write.API.Applications.Commands.ShopInfo;
using MediatR;

namespace B.S.DataBase.SuYuan.Write.API.Applications.CommandHandlers.ShopInfo
{
    public class ShopDanDelCommandHandler : IRequestHandler<ShopDanDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<ShopInfoModel> sRepo;

        public ShopDanDelCommandHandler(IBaseRepository<ShopInfoModel> sRepo)
        {
            this.sRepo = sRepo;
        }

        public async Task<APIResult<object>> Handle(ShopDanDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = sRepo.GetAll().Where(x => x.ShopId == request.ShopId).FirstOrDefault();
            if (model != null)
            {
                model.IsDel = true;
                await sRepo.Update(model);
                result.Code = ResultCode.删除成功;
                result.Msg = ResultCode.删除成功.ToString();
                result.Data = model;
            }
            else
            {
                result.Code = ResultCode.删除失败;
                result.Msg = ResultCode.删除失败.ToString();
            }
            return await Task.FromResult(result);
        }


    }
}
